package com.simple.elpmobile.onlinecase.ui;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.simple.elpmobile.onlinecase.R;
import com.simple.elpmobile.onlinecase.common.data.BaseGlobalVaribles;
import com.simple.elpmobile.onlinecase.engine.AppEngine;
import com.simple.elpmobile.onlinecase.engine.IManager;
import com.simple.elpmobile.onlinecase.engine.manager.NetworkManager;
import com.simple.elpmobile.onlinecase.engine.network.ResponseHandler;
import com.simple.elpmobile.onlinecase.engine.network.ResponseHandler.CommanHandler;
import com.simple.elpmobile.onlinecase.ui.base.BaseActivity;
import com.simple.elpmobile.onlinecase.ui.base.CustomToast;
import com.simple.elpmobile.onlinecase.ui.base.LoadingDialog;
import com.simple.elpmobile.onlinecase.utils.PreferencesUtil;

public class LoginActivity extends BaseActivity {
	private EditText loginNameTxt;
	private EditText loginPwdTxt;
	private Button loginBtn;
	private TextView registBtn;

	private LoadingDialog loadingDialog;

	public final static int SHOW_LOGIN = 0;
	public final static int TOKEN_EXPIRED = 1;
	private int showType = SHOW_LOGIN;
	public final static String SHOW_TYPE = "showType";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		showType = getIntent().getIntExtra(SHOW_TYPE, SHOW_LOGIN);
		initView();
	}

	private void initView() {
		loginNameTxt = (EditText) findViewById(R.id.login_name);
		loginPwdTxt = (EditText) findViewById(R.id.login_pwd);
		loginBtn = (Button) findViewById(R.id.login_btn);
		registBtn= (TextView) findViewById(R.id.login_bt);   
		registBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				Intent intent=new Intent(LoginActivity.this,RegistActivity.class);
				startActivity(intent);
			}
		});
		loginBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				execLogin();
			}
		});
		if (!PreferencesUtil.getString("name", "").equals("")) {
			loginNameTxt.setText(PreferencesUtil.getString("name", ""));
		}
		if (!PreferencesUtil.getString("pass", "").equals("")) {
			loginPwdTxt.setText(PreferencesUtil.getString("pass", ""));
		}
	}

	private void execLogin() {
		final String name=loginNameTxt.getText().toString().trim();
		final String pass=loginPwdTxt.getText().toString().trim();
		Pattern pattern = Pattern.compile("[0-9a-zA-Z]{1,23}",Pattern.CASE_INSENSITIVE); 
		Matcher matcher = pattern.matcher(name); 
		if (matcher.matches()) {
			if(TextUtils.isEmpty(pass)){
				CustomToast.showToast(getApplicationContext(), "密码不能为空", 1000);
			}else{
				showLoading("登陆中...");
				((NetworkManager) (AppEngine.getInstance()
						.getManager(IManager.NETWOTK_ID))).login(name, pass, new CommanHandler() {
							@Override
							public void onLoginSuccess(Object obj) {
								dismissLoading();
									gotoMain(obj);
							}

							@Override
							public void onLoginFailed(String errorDescription) {
								dismissLoading();
								CustomToast.showToast(getApplicationContext(),errorDescription, 1000);
							}
						});
			}
		}else{
			CustomToast.showToast(getApplicationContext(), "用户名只能存在字母", 1000);
		}
	}

	

	private void gotoMain(Object obj) {
		if (obj!=null) {
			Intent intent = new Intent(this, MainActivity.class);
			startActivity(intent);
			finish();
		}else{
			CustomToast.showToast(getApplicationContext(), "登陆失败！用户名或密码错误！", 1000);
		}
	}


	private final String DIALOG_LOCKER = "dialogLocker";

	private void showLoading(String msg) {
		synchronized (DIALOG_LOCKER) {
			if (loadingDialog != null) {
				loadingDialog.dismissDialog();
				loadingDialog = null;
			}
			loadingDialog = new LoadingDialog(this);
			loadingDialog.showDialog(msg);
		}
	}

	private void dismissLoading() {
		synchronized (DIALOG_LOCKER) {
			if (loadingDialog != null) {
				loadingDialog.dismissDialog();
				loadingDialog = null;
			}
		}
	}

	@Override
	public void onBackPressed() {
		if (showType == SHOW_LOGIN) {
			doubleClick();
		} else {
			super.onBackPressed();
		}
	}

	private static final long MAX_CLICK_TIME = 3 * 1000;
	private long firstClickTime;
	private long secondClickTime;

	private void doubleClick() {
		// 第一次点击
		if (firstClickTime == 0) {
			firstClickTime = System.currentTimeMillis();
			CustomToast.showToast(this, "再次点击退出", CustomToast.LENGTH_LONG);
		} else {
			secondClickTime = System.currentTimeMillis();

			// 超出最长约定时间，不退出应用
			if (secondClickTime - firstClickTime > MAX_CLICK_TIME) {
				firstClickTime = secondClickTime;
				CustomToast.showToast(this, "再次点击退出", CustomToast.LENGTH_LONG);
			} else {
				AppEngine.quitApplication(this);
			}
		}
	}

	@Override
	public byte activityId() {
		return LOGIN_ID;
	}

	public void onResume() {
		super.onResume();
	
	}

	public void onPause() {
		super.onPause();
		
	}

}